<?php
/**
 * Created by PhpStorm.
 * Author: huangwy
 * Date: 2018/4/3 17:42
 * Version: 1.0
 */

namespace app\admin\controller;
use app\admin\logic\CategoryLogic;

class Category extends Base
{
    public function index()
    {
        $category = db('category')->field('id,pid,name,o')->order('o asc')->select();

        $categoryLogic = new CategoryLogic();
        $category = $categoryLogic->getCatList($category);
        $this->assign('category', $category);
        return view();
    }

    public function del()
    {

        $id = input('id');

        $id = isset($id) ? intval($id) : false;
        if ($id) {
            $data['id'] = $id;
            $category = db('category');
            if ($category->where('pid=' . $id)->count()) {
                die('2');//存在子类，严禁删除。
            } else {
                $map = 'id=' . $id;
                $del = db('category')->where($map)->delete();
                addlog('删除分类，ID：' . $id);
                die('1');
            }

        } else {
            die('0');
        }

    }

    public function edit()
    {
        $id = input('id');
        $id = isset($id) ? intval($id) : false;
        $currentcategory = db('category')->where('id=' . $id)->find();
        $category = db('category')->field('id,pid,name')->where("id <> {$id}")->order('o asc')->select();
        $categoryLogic = new CategoryLogic();
        $category = $categoryLogic->getCatTree($currentcategory['pid'],$category);
        $this->assign('currentcategory', $currentcategory);
        $this->assign('category', $category);
        return view('form');
    }

    public function add()
    {

        $pid = isset($_GET['pid']) ? intval($_GET['pid']) : 0;
        $category = db('category')->field('id,pid,name')->order('o asc')->select();
        $categoryLogic = new CategoryLogic();
        $category = $categoryLogic->getCatTree($pid,$category);
        $this->assign('category', $category);
        return view('form');
    }

    public function update($act = null)
    {
        if ($act == 'order') {
            $id = input('post.id', 0, 'intval');
            if (!$id) {
                die('0');
            }
            $o = input('post.o', 0, 'intval');
            $categoryLogic = new CategoryLogic();
            $setOrder = $categoryLogic->setOrder($id,$o);
            die('1');
        }
        $id = input('post.id', false, 'intval');
        $data['type'] = input('post.type', 0, 'intval');
        $data['pid'] = input('post.pid', 0, 'intval');
        $data['name'] = input('post.name');
        $data['dir'] = input('post.dir','',array('strip_tags','trim'));
        $data['seotitle'] = input('post.seotitle', '', 'htmlspecialchars');
        $data['keywords'] = input('post.keywords', '', 'htmlspecialchars');
        $data['description'] = input('post.description', '', 'htmlspecialchars');
        $data['content'] = input('post.content');
        $data['url'] = input('post.url');
        $data['cattemplate'] = input('post.cattemplate');
        $data['contemplate'] = input('post.contemplate');
        $data['o'] = input('post.o', 0, 'intval');
        if ($data['name'] == '') {
            return json(['flag'=>1,'message'=>'分类名称不能为空!']);
        }
        if ($id) {
            if (db('category')->data($data)->where('id=' . $id)->update()) {
                addlog('文章分类修改，ID：' . $id . '，名称：' . $data['name']);
                return json(['flag'=>1,'message'=>'恭喜，分类修改成功!']);
                die(0);
            }
        } else {
            $id = db('category')->data($data)->insert();
            if ($id) {
                addlog('新增分类，ID：' . $id . '，名称：' . $data['name']);
                return json(['flag'=>0,'message'=>'恭喜，新增分类成功！!']);
                die(0);
            }
        }
        return json(['flag'=>0,'message'=>'恭喜，操作成功!']);
    }
}